package com.tfswx.jcyw.dmzc.api;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.resource.InputStreamResource;
import cn.hutool.core.util.IdUtil;
import com.tfswx.jcyw.dmzc.api.anno.Request;
import com.tfswx.jcyw.dmzc.api.dto.*;
import com.tfswx.jcyw.dmzc.api.dto.DmzcAddInputDTO.Zcqk;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ricky
 * @date 2023/3/13
 */
@Request("fwd")
public class FwdController {


    @Request("login")
    public LoginOutputDTO login(LoginInputDTO queryParam) {
        return ApiUtil.fwdApi.login(queryParam);
    }


    @Request("queryPsbzfllb")
    public List<PsbzfllbQueryResult> queryPsbzfllb(PsbzfllbQueryParam queryParam) {
        return ApiUtil.fwdApi.queryPsbzfllb(queryParam);
    }

    @Request("queryPsbzlb")
    public PageResult<PsbzlbQueryResult> queryPsbzlb(PageParam<PsbzlbQueryParam> queryParam) {
        return ApiUtil.fwdApi.queryPsbzlb(queryParam);
    }

    @Request("queryRylb")
    public PageResult<RylbQueryResult> queryRylb(PageParam<RylbQueryParam> queryParam) {
        return ApiUtil.fwdApi.queryRylb(queryParam);
    }

    @Request("addDmzc")
    public void addDmzc(DmzcAddInputDTO queryParam) {
        String gcmc = queryParam.getGcmc();
        String xmlx = queryParam.getXmlx();
        String bz = queryParam.getBz();
        String zcywjbh = queryParam.getZcywjbh();
        String psjhbh = queryParam.getPsjhbh();
        List<Zcqk> zcqks = queryParam.getZcqks();
        InputStreamResource jtwj = new InputStreamResource(new ByteArrayInputStream(Base64.decode(queryParam.getJtwj())), IdUtil.fastSimpleUUID().toUpperCase() + ".png");
        Map<String, Object> map = new HashMap<>();
        map.put("gcmc", gcmc);
        map.put("jtwj", jtwj);
//        map.put("psjhbh", psjhbh);
        map.put("xmlx", xmlx);
        map.put("bz", bz);
        map.put("zcywjbh", zcywjbh);

        for (int i = 0; i < zcqks.size(); i++) {
            Zcqk zcqk = zcqks.get(i);

            map.put("zcqks[" + i + "].psbzbm", zcqk.getPsbzbm());
            map.put("zcqks[" + i + "].zcyj", zcqk.getZcyj());
            map.put("zcqks[" + i + "].yjdj", zcqk.getYjdj());
            map.put("zcqks[" + i + "].kfrybm", zcqk.getKfrybm());
            map.put("zcqks[" + i + "].kfrymc", zcqk.getKfrymc());
        }

        ApiUtil.fwdApi.addDmzc(map);
    }

    @Request("addJhdmzc")
    public void addJhdmzc(DmzcAddInputDTO queryParam) {
        String gcmc = queryParam.getGcmc();
        String xmlx = queryParam.getXmlx();
        String bz = queryParam.getBz();
        String zcywjbh = queryParam.getZcywjbh();
        String psjhbh = queryParam.getPsjhbh();
        List<Zcqk> zcqks = queryParam.getZcqks();
        InputStreamResource jtwj = new InputStreamResource(new ByteArrayInputStream(Base64.decode(queryParam.getJtwj())), IdUtil.fastSimpleUUID().toUpperCase() + ".png");
        Map<String, Object> map = new HashMap<>();
        map.put("gcmc", gcmc);
        map.put("jtwj", jtwj);
        map.put("psjhbh", psjhbh);
        map.put("xmlx", xmlx);
        map.put("bz", bz);
        map.put("zcywjbh", zcywjbh);

        for (int i = 0; i < zcqks.size(); i++) {
            Zcqk zcqk = zcqks.get(i);

            map.put("zcqks[" + i + "].psbzbm", zcqk.getPsbzbm());
            map.put("zcqks[" + i + "].zcyj", zcqk.getZcyj());
            map.put("zcqks[" + i + "].yjdj", zcqk.getYjdj());
//            map.put("zcqks[" + i + "].kfrybm", zcqk.getKfrybm());
//            map.put("zcqks[" + i + "].kfrymc", zcqk.getKfrymc());
        }


        ApiUtil.fwdApi.addJhdmzc(map);
    }
}
